using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Linq;

namespace WcfService1.DTO
{
    [DataContract]
    public class InventoryDTO : INotifyPropertyChanged
    {
        private String _CoaHpp;
        private String _CoaInventory;
        private String _Description;
        private bool _IsStock;
        private ItemDTO _Item;
        private String _KodeMasuk;
        private string _KodePallet;
        private Decimal _Lebar;
        private int _No;
        private int _NoBundle;
        private Decimal _Panjang;
        private Decimal _Qty;
        private Decimal _QtyPack;
        private Decimal _QtyUnit;
        private SatuanDTO _SatuanPack;
        private SatuanDTO _SatuanUnit;
        private String _SerialNumber;
        private string _SubGroup;
        private Decimal _Tinggi;
        private Decimal _UnitPrice;
        private ObservableCollection<WarehouseDTO> _Warehouses;
        //private ObservableCollection<InventoryWarehouseDTO> _inventoryWarehouses;
        private String _created_by;
        private DateTime? _created_date;
        private string _group;
        private String _modified_by;
        private DateTime? _modified_date;
        private decimal _TotalValue;
        private int _NoBundle2;
        private decimal _QtyOnHand;
        private string _CoaUnbilled;
        private string _CoaPurchaseReturn;
        private string _CoaExpense;
        private string _CoaSales;
        private string _CoaInTransit;
        private string _CoaSalesReturn;
        private string _Pallet;
        private string _KodeWarehouse;

        [DataMember]
        public String SerialNumber
        {
            get { return _SerialNumber; }
            set
            {
                if (value == SerialNumber) return;
                _SerialNumber = value;
                RaisePropertyChanged("SerialNumber");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                if (value == Description) return;
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ItemDTO Item
        {
            get { return _Item; }
            set
            {
                if (ReferenceEquals(Item,value) || value == null) return;
                _Item = value;
                RaisePropertyChanged("Item");
            }
        }


        [DataMember]
        public Decimal QtyPack
        {
            get { return _QtyPack; }
            set
            {
                if (value == QtyPack) return;
                _QtyPack = value;
                RaisePropertyChanged("QtyPack");
            }
        }


        [DataMember]
        public Decimal QtyUnit
        {
            get { return _QtyUnit; }
            set
            {
                if (value == QtyUnit) return;
                _QtyUnit = value;
                RaisePropertyChanged("QtyUnit");
            }
        }


        [DataMember]
        public Decimal UnitPrice
        {
            get { return _UnitPrice; }
            set
            {
                if (value == UnitPrice) return;
                _UnitPrice = value;
                RaisePropertyChanged("UnitPrice");
            }
        }

        [DataMember]
        public Decimal TotalValue
        {
            get { return _TotalValue; }
            set
            {
                if (value == TotalValue) return;
                _TotalValue = value;
                RaisePropertyChanged("TotalValue");
            }
        }


        [DataMember]
        public SatuanDTO SatuanUnit
        {
            get { return _SatuanUnit; }
            set
            {
                if (value == SatuanUnit) return;
                _SatuanUnit = value;
                RaisePropertyChanged("SatuanUnit");
            }
        }


        [DataMember]
        public SatuanDTO SatuanPack
        {
            get { return _SatuanPack; }
            set
            {
                if (value == SatuanPack) return;
                _SatuanPack = value;
                RaisePropertyChanged("SatuanPack");
            }
        }


        [DataMember]
        public Decimal Panjang
        {
            get { return _Panjang; }
            set
            {
                if (value == Panjang) return;
                _Panjang = value;
                RaisePropertyChanged("Panjang");
            }
        }


        [DataMember]
        public Decimal Lebar
        {
            get { return _Lebar; }
            set
            {
                if (value == Lebar) return;
                _Lebar = value;
                RaisePropertyChanged("Lebar");
            }
        }


        [DataMember]
        public Decimal Tinggi
        {
            get { return _Tinggi; }
            set
            {
                if (value == Tinggi) return;
                _Tinggi = value;
                RaisePropertyChanged("Tinggi");
            }
        }


        [DataMember]
        public String KodeMasuk
        {
            get { return _KodeMasuk; }
            set
            {
                if (value == KodeMasuk) return;
                _KodeMasuk = value;
                RaisePropertyChanged("KodeMasuk");
            }
        }


        [DataMember]
        public ObservableCollection<WarehouseDTO> Warehouses
        {
            get { return _Warehouses; }
            set
            {
                if (value == Warehouses) return;
                _Warehouses = value;
                RaisePropertyChanged("Warehouses");
            }
        }


        [DataMember]
        public string KodePallet
        {
            get { return _KodePallet; }
            set
            {
                if (value == KodePallet) return;
                _KodePallet = value;
                RaisePropertyChanged("KodePallet");
            }
        }


        [DataMember]
        public String created_by
        {
            get { return _created_by; }
            set
            {
                _created_by = value;
                RaisePropertyChanged("created_by");
            }
        }


        [DataMember]
        public DateTime? created_date
        {
            get { return _created_date; }
            set
            {
                _created_date = value;
                RaisePropertyChanged("created_date");
            }
        }

        [DataMember]
        public String modified_by
        {
            get { return _modified_by; }
            set
            {
                _modified_by = value;
                RaisePropertyChanged("modified_by");
            }
        }


        [DataMember]
        public DateTime? modified_date
        {
            get { return _modified_date; }
            set
            {
                _modified_date = value;
                RaisePropertyChanged("modified_date");
            }
        }

        [DataMember]
        public int NoBundle
        {
            get { return _NoBundle; }
            set
            {
                if (value == NoBundle) return;
                _NoBundle = value;
                RaisePropertyChanged("NoBundle");
            }
        }

        [DataMember]
        public int NoBundle2
        {
            get { return _NoBundle2; }
            set
            {
                if (value == NoBundle2) return;
                _NoBundle2 = value;
                RaisePropertyChanged("NoBundle2");
            }
        }



        [DataMember]
        public String CoaHpp
        {
            get { return _CoaHpp; }
            set
            {
                if(String.IsNullOrEmpty(value) || value == CoaHpp)return;
                _CoaHpp = value.Split(new string[]{":"},StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaHpp");
            }
        }


        [DataMember]
        public String CoaInventory
        {
            get { return _CoaInventory; }
            set
            {
                if (String.IsNullOrEmpty(value) || value == CoaInventory) return;
                _CoaInventory = value.Split(new string[] { ":" }, StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaInventory");
            }
        }


        [DataMember]
        public bool IsStock
        {
            get { return _IsStock; }
            set
            {
                if (value == IsStock) return;
                _IsStock = value;
                RaisePropertyChanged("IsStock");
            }
        }


        public int No
        {
            get { return _No; }
            set
            {
                if (No == value) return;
                _No = value;
                RaisePropertyChanged("No");
            }
        }


        [DataMember]
        public string Subgroup
        {
            get
            {
                //if (Item == null) return string.Empty;
                //return Item.SubGroup != null ? Item.SubGroup.SubGroupName :  string.Empty;
                return _SubGroup;
            }
            set
            {
                if (Subgroup == value) return;
                _SubGroup = value;
                RaisePropertyChanged("Subgroup");
            }
        }


        [DataMember]
        public string Group
        {
            get
            {
                //if (Item == null) return string.Empty;
                //return Item.SubGroup != null ? Item.SubGroup.Group.GroupName : string.Empty;
                return _group;
            }
            set
            {
                if (Group == value) return;
                _group = value;
                RaisePropertyChanged("Group");
            }
        }

        [DataMember]
        public Decimal QtyOnHand
        {
            get
            {
                return _QtyOnHand;
            }

            set
            {
                if (QtyOnHand == value) return;
                _QtyOnHand = value;
                RaisePropertyChanged("QtyOnHand");
            }
        }

        [DataMember]
        public string KodeWarehouse
        {
            get
            {
                return _KodeWarehouse;
            }

            set
            {
                if (KodeWarehouse == value) return;
                _KodeWarehouse = value;
                RaisePropertyChanged("KodeWarehouse");
            }
        }

        [DataMember]
        public string Pallet
        {
            get
            {
                return _Pallet;
            }

            set
            {
                if (Pallet == value) return;
                _Pallet = value;
                RaisePropertyChanged("Pallet");
            }
        }


        [DataMember]
        public string CoaSales
        {
            get
            {
                return _CoaSales;
            }

            set
            {
                if (String.IsNullOrEmpty(value) || value == CoaSales) return;
                _CoaSales = value.Split(new string[] { ":" }, StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaSales");
            }
        }

        [DataMember]
        public string CoaSalesReturn
        {
            get
            {
                return _CoaSalesReturn;
            }

            set
            {
                if (String.IsNullOrEmpty(value) || value == CoaSalesReturn) return;
                _CoaSalesReturn = value.Split(new string[] { ":" }, StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaSalesReturn");
            }
        }

        [DataMember]
        public string CoaPurchaseReturn
        {
            get
            {
                return _CoaPurchaseReturn;
            }

            set
            {
                if (String.IsNullOrEmpty(value) || value == CoaPurchaseReturn) return;
                _CoaPurchaseReturn = value.Split(new string[] { ":" }, StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaPurchaseReturn");
            }
        }


        [DataMember]
        public string CoaExpense
        {
            get
            {
                return _CoaExpense;
            }

            set
            {
                if (String.IsNullOrEmpty(value) || value == CoaExpense) return;
                _CoaExpense = value.Split(new string[] { ":" }, StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaExpense");
            }
        }

        [DataMember]
        public string CoaInTransit
        {
            get
            {
                return _CoaInTransit;
            }

            set
            {
                if (String.IsNullOrEmpty(value) || value == CoaInTransit) return;
                _CoaInTransit = value.Split(new string[] { ":" }, StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaInTransit");
            }
        }


        [DataMember]
        public string CoaUnbilled
        {
            get
            {
                return _CoaUnbilled;
            }

            set
            {
                if (String.IsNullOrEmpty(value) || value == CoaUnbilled) return;
                _CoaUnbilled = value.Split(new string[] { ":" }, StringSplitOptions.None).LastOrDefault();
                RaisePropertyChanged("CoaUnbilled");
            }
        }
        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }

        public void GetQtyOnHand(String KodeWarehouse)
        {
            if (Warehouses != null)
                if  (Warehouses.FirstOrDefault(c => c.KodeWarehouse == KodeWarehouse) != null)
                    this.QtyOnHand = Warehouses.FirstOrDefault(c => c.KodeWarehouse == KodeWarehouse).Qty;
        }
    }
}